package com.googlecode.stemwijzer.shared.model.impl;

import java.util.HashMap;

import com.googlecode.stemwijzer.shared.data.QuestionnaireData;
import com.googlecode.stemwijzer.shared.model.IsAnswer;
import com.googlecode.stemwijzer.shared.model.IsQuestion;
import com.googlecode.stemwijzer.shared.model.IsQuestionnaireResult;

public class QuestionnaireResult extends DomainObject implements IsQuestionnaireResult {
	private final String name;
	private final HashMap<IsQuestion, IsAnswer> data;

	public QuestionnaireResult() {
		this(null, null);
	}

	public QuestionnaireResult(final String name, final HashMap<IsQuestion, IsAnswer> data) {
		this.name = name;
		this.data = data;
	}

	public IsAnswer getAnswer(final IsQuestion question) {
		if (question == null) {
			return null;
		}
		return this.data.get(question);
	}

	@Override
	public Integer getDifference(final IsQuestionnaireResult other) {
		Integer result = 0;

		for (IsQuestion q : QuestionnaireData.getAvailableQuestions()) {
			IsAnswer a1 = this.data.get(q);
			IsAnswer a2 = other.getAnswer(q);

			if ((a1 != null) && (a2 != null)) {
				result += Math.abs(a1.getValue() - a2.getValue());
			}
		}
		return result;
	}

	public String getName() {
		return this.name;
	}

	@Override
	public void setAnswer(final IsQuestion question, final IsAnswer answer) {
		this.data.put(question, answer);
		System.out.println("Vraag [" + question.toString() + "], antwoord [" + answer.toString() + "]");
	}
}
